package com.urbo.android.shortcutter;


import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class SmsDialog extends UrboDialogActivity {

	private static final int PICK_PHONE_NUMBER_REQUEST_CODE = 99001; 
	private static final int PICK_EMAIL_ADDRESS_REQUEST_CODE = 99002; 
	
	public static String PHONE_NUMBER = "phone_number"; 
	public static String MESSAGE_TEXT = "message_text";
	public static String SUBJECT = "subject";
	
	public static final String DIALOG_TYPE_KEY = "dialog_type";
	public static final int DIALOG_SMS = 991001;
	public static final int DIALOG_EMAIL = 991002;
	
	private int dialogType;
	
	private void setContactTitle(String title) {
		((TextView)findViewById(R.id.text_sms_phone_number)).setText( title );
	}
	
	private void setContactInputType(int type) {
		((EditText)findViewById(R.id.edit_phone_number)).setInputType( type );
	}
	
	private void setContactText(String text) {
		((EditText)findViewById(R.id.edit_phone_number)).setText( text );
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms_dialog, R.id.sms_layout);
		((Button)findViewById(R.id.btn_sms_getnumber)).setOnClickListener(findClickListener);
	}
	
	private void setSubjectVisibility(int visibility) {
		((TextView)findViewById(R.id.text_email_subject)).setVisibility( visibility );
		((EditText)findViewById(R.id.edit_email_subject)).setVisibility( visibility );
		findViewById(R.id.sms_inner_layout).requestLayout();
		//findViewById(R.id.sms_inner_layout).forceLayout(); // I still don't get meaning of the force
		
	}
	
	@Override
	protected void onStart()
	{
		super.onStart();
		Bundle dataFromCaller = getIntent().getExtras();
		dialogType = dataFromCaller.getInt(DIALOG_TYPE_KEY);
		
		switch (dialogType) {
			case DIALOG_SMS:
				setContactTitle("Phone number");
				setContactInputType(InputType.TYPE_CLASS_PHONE);
				setSubjectVisibility( View.GONE );
				break;
				
			case DIALOG_EMAIL:
				setContactTitle("Email address");
				setSubjectVisibility( View.VISIBLE );
				setContactInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
				break;
		}
	}

	protected void setReturnData() {
		Bundle result = new Bundle();
		result.putString(PHONE_NUMBER,((EditText)findViewById(R.id.edit_phone_number)).getText().toString());
		result.putString(SUBJECT,((EditText)findViewById(R.id.edit_email_subject)).getText().toString());
		result.putString(MESSAGE_TEXT,((EditText)findViewById(R.id.edit_message_text)).getText().toString());
		putExtra(RESULT_BUNDLE, result);
	}

	private Button.OnClickListener findClickListener = new Button.OnClickListener(){
		@Override
		public void onClick(View v) {
			
			Intent contactPickerIntent;
			
			switch (dialogType) {
				case DIALOG_SMS:
					contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
					startActivityForResult(contactPickerIntent, PICK_PHONE_NUMBER_REQUEST_CODE );
					break;
					
				case DIALOG_EMAIL:
					contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
					startActivityForResult(contactPickerIntent, PICK_EMAIL_ADDRESS_REQUEST_CODE);
					break;
			}
		}
	};
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
		if (resultCode == RESULT_OK) {
			switch (requestCode) {  
				case PICK_PHONE_NUMBER_REQUEST_CODE :  
					setContactText(ContentUtils.getPhoneNumberFromPhoneUri(this, data.getData()));
					break;  
				case PICK_EMAIL_ADDRESS_REQUEST_CODE :
					setContactText(ContentUtils.getEmailFromContactUri(this, data.getData()));
					break;
			}  
		}
	} 
}
